#-*- coding:utf-8 -*-
'''
Created on Oct 18, 2012

@author: johnny
'''
import os
from optparse import OptionParser
from job import job


def main(): 
    parser = OptionParser()
    parser.add_option('-a', '--all', action='store_true',
                      dest='load_all', help="load all tests to job")
    parser.add_option('-n', '--name', action='store', dest='names',
           help="Add job by case name, list case names separate by comma")
    
    parser.add_option('-f', '--file', action='store', dest='filename',
           help="Read test cases name from file, each line as a test")
    
    (options, args) = parser.parse_args()
    
    manager = job.JobManager()
    
    if options.load_all:
        manager.add_jobs(job.load_all_jobs())
        manager.start_jobs()
    elif options.names:
        name_list = options.names.split(',')
        job_list = []
        for name in name_list:
            job_list.append(job.Job(name))
        
        manager.add_jobs(job_list)
        manager.start_jobs()
    elif options.filename:
        if os.path.isfile(options.filename):
            fl = open(options.filename, 'r')
            lines = fl.readlines()
            fl.close()
            lines = [x.strip() for x in lines]

            job_list = []
            for n in lines:
                job_list.append(job.Job(n))
            
            manager.add_jobs(job_list)
            manager.start_jobs()
            
        else:
            print "Cannot find this file"

if __name__ == '__main__':
    main()